/* Copyright 2009 Requirements Management System 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.mags.remas.view.widgets;

import java.util.Date;
import org.apache.commons.validator.GenericValidator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.mags.object.common.ParserFormatterUtils;
import org.mags.remas.view.commons.ImageProvider;
import org.mags.remas.view.dialogs.CalendarDialog;
import org.vafada.swtcalendar.SWTCalendarEvent;
import org.vafada.swtcalendar.SWTCalendarListener;

/**
 * 
 * @author Mario Gaitan
 *
 */
public class InputMaker {
	private Display display;
	private Label lastLabelAdded;
	private Button lastButtonAdded;
	public Button getLastButtonAdded() {
		return lastButtonAdded;
	}
	public Display getDisplay() {
		return display;
	}
	public void setDisplay(Display display) {
		this.display = display;
	}
	public Label getLastLabelAdded() {
		return lastLabelAdded;
	}
	public Composite addContainer(Composite parent, String title) {
		GridData containerGridData = new GridData();
		containerGridData.horizontalAlignment = SWT.FILL;
		containerGridData.grabExcessHorizontalSpace = true;
		containerGridData.verticalAlignment = SWT.FILL;	
		containerGridData.grabExcessVerticalSpace=false;
		Group container = new Group(parent, SWT.SHADOW_ETCHED_IN);
		container.setLayout(new GridLayout(4, true));
		container.setLayoutData(containerGridData);
		if(!GenericValidator.isBlankOrNull(title))
			container.setText(title);
		return container;
	}
	
	
	public Label addReadText(Composite parent, String label) {
		int labelSize = (label==null)?0:label.length();
		int labelSpan = 1;
		int controlSpan = 3;
		if(labelSize>60) {
			labelSpan = 3;
			controlSpan = 1;			
		} else if(labelSize>30) {
			labelSpan = 2;
			controlSpan = 2;			
		}
		Label lblLabel = (Label)addControl(parent, "LABEL", labelSpan);
		lblLabel.setText(label);
		Label lblControl = (Label)addControl(parent, "READTEXT", controlSpan);
		return lblControl;
	}
	public Text addText(Composite parent, String label, int textLimit) {
		int labelSize = (label==null)?0:label.length();
		int labelSpan = 1;
		int controlSpan = 3;
		if(labelSize>60) {
			labelSpan = 3;
			controlSpan = 1;			
		} else if(labelSize>30) {
			labelSpan = 2;
			controlSpan = 2;			
		}
		Label lblLabel = (Label)addControl(parent, "LABEL", labelSpan);
		lblLabel.setText(label);		
		Text txtText = (Text)addControl(parent, "TEXT", controlSpan);
		txtText.setTextLimit(textLimit);
		return txtText;
	}	
	public Combo addCombo(Composite parent, String label) {
		int labelSize = (label==null)?0:label.length();
		int labelSpan = 1;
		int controlSpan = 3;
		if(labelSize>60) {
			labelSpan = 3;
			controlSpan = 1;			
		} else if(labelSize>30) {
			labelSpan = 2;
			controlSpan = 2;			
		}
		Label lblLabel = (Label)addControl(parent, "LABEL", labelSpan);
		lblLabel.setText(label);
		Combo cmbCombo = (Combo)addControl(parent, "COMBO", controlSpan);
		return cmbCombo;
	}
	
	
	
	public Label addTitle(Composite parent, String title) {
		Label lblTitle = (Label)addControl(parent, "TITLE");
		lblTitle.setText(title);
		return lblTitle;
	}	
	public Text addTextDate(Composite parent, String label) {
		Label lblLabel = (Label)addControl(parent, "LABEL");
		lblLabel.setText(label);		
		Text txtText = (Text)addControl(parent, "TEXTDATE");
		txtText.setTextLimit(10);
		return txtText;		
	}
	public Text addTextArea(final Composite parent, String label, int textLimit) {
		Label lblLabel = (Label)addControl(parent, "LABEL");
		lblLabel.setText(label);				
		final Text txtText = (Text)addControl(parent, "TEXTAREA");
		txtText.setTextLimit(textLimit);
		return txtText;
	}	
	public ListValueViewer addListValue(final Composite parent, String label) {
		Label lblLabel = (Label)addControl(parent, "LABEL");
		lblLabel.setText(label);
		ListValueViewer listValue = (ListValueViewer)addControl(parent, "LISTVALUE");
		return listValue;
	}
	
	
	private void addToolBar(Composite parent, final ListValueViewer listValue) {
		ToolBar toolBar = new ToolBar(parent, SWT.VERTICAL);
		ToolItem button;
		button = new ToolItem(toolBar, SWT.NONE);
		button.setImage(ImageProvider.getCommonImage("new.png"));
		button.setToolTipText("new");
		button.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
			@Override
			public void widgetSelected(SelectionEvent e) {
				listValue.getCreateAction().run();
				
			}
		});
		button = new ToolItem(toolBar, SWT.NONE);
		button.setImage(ImageProvider.getCommonImage("edit.png"));
		button.setToolTipText("edit");
		button.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
			@Override
			public void widgetSelected(SelectionEvent e) {
				listValue.getEditAction().run();
			}
		});						
		button = new ToolItem(toolBar, SWT.NONE);
		button.setImage(ImageProvider.getCommonImage("delete.png"));
		button.setToolTipText("delete");
		button.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
			@Override
			public void widgetSelected(SelectionEvent e) {
				listValue.getDeleteAction().run();
			}
		});		
		button = new ToolItem(toolBar, SWT.NONE);
		button.setImage(ImageProvider.getCommonImage("refresh.png"));
		button.setToolTipText("refresh");
		button.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
			@Override
			public void widgetSelected(SelectionEvent e) {
				listValue.getRefreshAction().run();
			}
		});
		button = new ToolItem(toolBar, SWT.NONE);
		button.setToolTipText("expand");
		button.setImage(ImageProvider.getCommonImage("expand.png"));
		button.addSelectionListener(new SelectionListener() {
			public void widgetDefaultSelected(SelectionEvent e) {
			}
			public void widgetSelected(SelectionEvent e) {
				listValue.getExpandAction().run();
			}
		});		
	}
	public Object addControl(Composite parent, String type) {
		return addControl(parent, type, -1);
	}
	public Object addControl(Composite parent, String type, int horizontalSpan) {
		Control w = null;
		GridData gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = true;
		if(type.equalsIgnoreCase("COMBO")) {
			gridData.horizontalSpan = (horizontalSpan>0)?horizontalSpan:3;									
			w = new Combo(parent, SWT.BORDER|SWT.DROP_DOWN|SWT.READ_ONLY);
			w.setLayoutData(gridData);
		} else if(type.equalsIgnoreCase("TEXT")) {
			gridData.horizontalSpan = (horizontalSpan>0)?horizontalSpan:3;						
			w = new Text(parent, SWT.BORDER);
			w.setLayoutData(gridData);
		} else if(type.equalsIgnoreCase("READTEXT")) {
			gridData.horizontalSpan = (horizontalSpan>0)?horizontalSpan:3;			
			w = new Label(parent, SWT.BORDER);
			w.setLayoutData(gridData);
		} else if(type.equalsIgnoreCase("TITLE")) {
			gridData.horizontalAlignment = SWT.CENTER;
			gridData.horizontalSpan = (horizontalSpan>0)?horizontalSpan:4;			
			w = new Label(parent, SWT.NONE);
			w.setLayoutData(gridData);
		} else if(type.equalsIgnoreCase("LABEL")) {
			gridData.horizontalSpan = (horizontalSpan>0)?horizontalSpan:1;	
			w = new Label(parent, SWT.NONE);
			w.setLayoutData(gridData);
			lastLabelAdded = (Label)w;
		} else if(type.equalsIgnoreCase("LISTVALUE")) {
			gridData.verticalAlignment = SWT.BEGINNING;	
			gridData.horizontalSpan = (horizontalSpan>0)?horizontalSpan:3;
			gridData.verticalSpan = 4;
			gridData.grabExcessVerticalSpace=false;
			gridData.heightHint = 110;
			Composite composite = new Composite(parent, SWT.BORDER);
			composite.setLayoutData(gridData);
			GridLayout gridLayout = new GridLayout();
			gridLayout.numColumns = 2;
			gridLayout.marginHeight = 0;
			gridLayout.marginWidth = 0;
			gridLayout.verticalSpacing = 0;
			gridLayout.horizontalSpacing = 0;
			composite.setLayout(gridLayout);
			GridData gd = new GridData();
			gd.grabExcessHorizontalSpace = true;
			gd.grabExcessVerticalSpace = true;
			gd.verticalAlignment = SWT.FILL;
			gd.horizontalAlignment = SWT.FILL;
			ListValueViewer object = new ListValueViewer(composite, SWT.NONE);
			object.getControl().setLayoutData(gd);
			addToolBar(composite, object);
			w = object.getControl();
			for(int i=0;i<=6;i++)
				addControl(parent, "LABEL");
			return object;
		} else if(type.equalsIgnoreCase("TEXTAREA")) {
			gridData.verticalAlignment = SWT.BEGINNING;	
			gridData.horizontalSpan = (horizontalSpan>0)?horizontalSpan:3;
			gridData.verticalSpan = 4;
			gridData.grabExcessVerticalSpace=false;
			gridData.heightHint = 75;
			w = new Text(parent, SWT.BORDER|SWT.MULTI|SWT.V_SCROLL);
			w.setLayoutData(gridData);
			addControl(parent, "LABEL");
			addControl(parent, "LABEL");
			addControl(parent, "LABEL");
		} else if(type.equalsIgnoreCase("TEXTDATE")) {
			gridData.horizontalSpan = (horizontalSpan>0)?horizontalSpan:2;						
			w = new Text(parent, SWT.BORDER);
			w.setLayoutData(gridData);	
			GridData buttonDateGrid = new GridData();
			buttonDateGrid.horizontalAlignment = SWT.FILL;
			buttonDateGrid.grabExcessHorizontalSpace = true;
			final Text txtDateTextField = (Text)w;
			Button button = new Button(parent, SWT.NONE);
			lastButtonAdded = button;
			button.setImage(ImageProvider.getCommonImage("calendar.png"));
			button.setLayoutData(buttonDateGrid);
			button.addSelectionListener(new SelectionListener(){
				public void widgetDefaultSelected(SelectionEvent e) {
				}
				public void widgetSelected(SelectionEvent e) {
					final CalendarDialog calendar = new CalendarDialog(display);
					calendar.addDateChangedListener(new SWTCalendarListener() {
						public void dateChanged(SWTCalendarEvent event) {
							Date date = event.getCalendar().getTime();
							txtDateTextField.setText(ParserFormatterUtils.getInstance().date2Text(date));
						}
					});							
					calendar.open();
				}
			});			
		}
		return w;
	}	
}
